package com.uking.udppush;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;
import com.uking.helper.VKLogger;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UdpHelper implements Runnable {
    public static final String ACTION_RECEIVE_UDP_MSG = "com.uking.ukingmodule.udpmessage";
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "UDP Demo":"UDP Demo";
    public static final String SERVERIP = "127.0.0.1";
    public Boolean IsThreadDisable = false;
    DatagramSocket datagramSocket;
    private long lastSendMsg;
    private Context mContext;
    InetAddress mInetAddress;
    private int port = 8888;

    public UdpHelper(Context context, WifiManager manager, int port) {
        this.mContext = context;
        this.port = port;
    }

    public void StartListen() {
        byte[] message = new byte[548];
        try {
            this.datagramSocket = new DatagramSocket(this.port, InetAddress.getByName(SERVERIP));
            this.datagramSocket.setBroadcast(true);
            DatagramPacket datagramPacket = new DatagramPacket(message, message.length);
            while (!IsThreadDisable) {
                try {
                    VKLogger.d(TAG, "\u51c6\u5907\u63a5\u53d7");
                    this.datagramSocket.receive(datagramPacket);
                    String strMsg = new String(datagramPacket.getData(), datagramPacket.getOffset(), datagramPacket.getLength()).trim();
                    VKLogger.d(TAG, "\u6765\u81ea" + datagramPacket.getAddress().getHostAddress().toString() + "\u7684\u6d88\u606f  :" + strMsg);
                    if (System.currentTimeMillis() - this.lastSendMsg > 3000) {
                        VKLogger.d(TAG, "\u95f4\u96943S\u53d1\u9001");
                        this.lastSendMsg = System.currentTimeMillis();
                        Intent intent = new Intent(ACTION_RECEIVE_UDP_MSG);
                        intent.putExtra("udp_message", strMsg);
                        this.mContext.sendBroadcast(intent);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return;
                }
            }
        } catch (SocketException e2) {
            e2.printStackTrace();
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        }
    }

    public void send(String message) {
        if (message == null) {
            message = "Hello IdeasAndroid!";
        }
        int server_port = this.port;
        LogUtil.d(TAG, "UDP\u53d1\u9001\u6570\u636e:" + message);
        DatagramSocket s = null;
        try {
            s = new DatagramSocket();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        InetAddress local = null;
        try {
            local = InetAddress.getByName("255.255.255.255");
        } catch (UnknownHostException e2) {
            e2.printStackTrace();
        }
        try {
            s.send(new DatagramPacket(message.getBytes(), message.length(), local, server_port));
            s.close();
        } catch (IOException e3) {
            e3.printStackTrace();
        }
    }

    public void run() {
        LogUtil.d(TAG, "start liste Udp Message");
        StartListen();
    }

    public void stop() {
        LogUtil.d(TAG, "stop liste Udp Message");
        this.IsThreadDisable = true;
        if (this.datagramSocket != null) {
            this.datagramSocket.close();
        }
    }
}
